import os
import time

from selenium.webdriver.support.wait import WebDriverWait

from config import DIR_PATH
from tools.logger import logger
from utils import UtilsDriver

# 创建的基类
# 创建对象库层的基类
class BasePage:
    def __init__(self):
        self.driver=UtilsDriver.get_driver()

    def get_element(self,location):
        wait=WebDriverWait(self.driver,10,1)
        element=wait.until(lambda x:x.find_element(*location))
        return element

    # 切换进iframe表单
    def SwitchFrame(self, ID):
        self.driver.switch_to.frame(ID)

    #app查找元素
    def base_find(self,location,timeout=10,poll=0.5):
        logger.info("正在查找元素:{}".format(location))
        return (WebDriverWait(self.driver,timeout,poll)
                .until(lambda x:x.find_element(*location)))

    def base_click(self,loc):
        logger.info("正调用点击元素:{}".format(loc))
        self.base_find(loc).click()

    def base_input(self,loc,value):
        logger.info("正调用输入元素:{}".format(loc))
        el=self.base_find(loc)
        el.clear()
        el.send_keys(value)

    def base_get_text(self,loc):
        logger.info("正调用获取文本元素:{}".format(loc))
        return self.base_find(loc).text()

    def base_get_img(self):
        logger.info("正调用截图方法")
        img_path = (DIR_PATH+os.sep+'img'+os.sep+"{}.png"
                    .format(time.strftime('%Y%m%d%H%M%S')))
        self.driver.get_screenshot_as_file(img_path)

    #切换frame
    def base_switch_frame(self, loc):
        logger.info("正在调用切换frame方法，切换对象:{}".format(loc))
        el=self.base_find(loc)
        self.driver.switch_to.frame(el)

    def base_default_frame(self):
        self.driver.switch_to.default_content()


# 创建操作层的基类
class BaseHandle:
    def input_text(self,element,text):
        element.clear()
        element.send_keys(text)






